package com.ruoyi.web.controller.sale;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.framework.util.ShiroUtils;
import com.ruoyi.system.domain.*;
import com.ruoyi.system.domain.aixsVo.AllVo;
import com.ruoyi.system.mapper.SysDeptMapper;
import com.ruoyi.system.mapper.SysRoleDeptMapper;
import com.ruoyi.system.service.AixsService;
import com.ruoyi.system.service.ISysDeptService;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author Mr.Keshihao
 * @Date 2020/11/14 8:42
 * @Version 1.0
 */
@Controller
@RequestMapping("/sale/detail")
public class saleController extends BaseController {

  private String prefix = "sale/aixs";

  @RequiresPermissions("sale:detail:view")
  @GetMapping()
  public String detail()
  {
    return prefix + "/detail";
  }

  @Autowired
  private AixsService aixsService;

  @Autowired
  private ISysDeptService iSysDeptService;

  @Autowired
  private SysDeptMapper deptMapper;

  @Autowired
  private SysRoleDeptMapper roleDeptMapper;



  @GetMapping("/add")
  public String add()
  {
    return prefix + "/add";
  }

  @PostMapping("/add")
  @ResponseBody
  public AjaxResult addSave(String target)
  {
    return toAjax(1);
  }



  @GetMapping("/bfb/{id}")
  public String bfb(@PathVariable("id") String id, ModelMap modelMap){
    modelMap.put("id",id);
    return prefix+"/bfb";
  }

  @PostMapping("/setbfb")
  @ResponseBody
  public AjaxResult bfb(String id, double bfb){
    return toAjax(1);
  }


  @ApiOperation("获取人员销售详情")
  @RequiresPermissions("sale:detail:list")
  @PostMapping("/list")
  @ResponseBody
  public TableDataInfo list(Aixs aixs)
  {

    // 取出登录用户信息
    SysUser user = (SysUser) SecurityUtils.getSubject().getPrincipal();
    // 是否拥有超级管理员标志
    Boolean flag = false;
    List<SysRole> roles = user.getRoles();

    Long roleId = roles.get(0).getRoleId();
    for (SysRole role : roles) {
      if (role.getRoleId().equals(1L)) flag = true;
    }

    if (flag.equals(false)) {
      Long deptId = user.getDeptId();
      SysDept sysDept = iSysDeptService.selectDeptById(deptId);

      List<Long> deptList = roleDeptMapper.selectDeptListByRoleId(roleId);

      List<String> deptNames = new ArrayList<>();

      if (deptList.size() != 0) {
        deptNames = deptMapper.getDeptNameByList(deptList);
        aixs.setDeptNames(deptNames);
      }

    }

    startPage();
    List<Aixs> list = aixsService.selectSaleDetail(aixs);

    for (Aixs aixs1 : list) {
      if (aixs1.get药品名称() == null){
        aixs1.set药品名称("所有");
      }
    }

    AllVo allVo = aixsService.getPersonAllPrice(aixs);

    Double sum = allVo.getAllPrice();

    sum = (double)Math.round(sum*100)/100;

    Double allProfit = allVo.getAllProfit();

    allProfit = (double)Math.round(allProfit*100)/100;

    for (int i = 0; i < list.size(); i++) {
      if (i == 0) {
        Aixs aixs1 = list.get(0);
        aixs1.setAllPersonPrice(sum);
        aixs1.setAllProfit(allProfit);
      } else {
        Aixs aixs1 = list.get(i);
        aixs1.setAllPersonPrice(0D);
        aixs1.setAllProfit(0D);
      }
    }

    TableDataInfo dataTable = getDataTable(list);

    return dataTable;
  }

}
